// Copyright (C) 2011  Andrew H. Chan
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

#include "input_data.h"
#include "util.h"

using namespace std;

int Input_data::read_input(std::string const& file_name) {
    ifstream in(file_name.c_str());
    if(!in.is_open()) {
        return 1;
    }

    // read header
    in >> num_haps;
    in >> segsites; 
    string line;

    // chomp newline from first line
    getline(in, line);

    // read in genotypes
    genotypes.resize(num_haps/2);
    for(uint i=0; i<num_haps/2; ++i) {
        getline(in, line);
        genotypes[i].resize(segsites);
        for(uint j=0; j<segsites; ++j) {
            if(line[j] == '0') {
                genotypes[i][j] = 0;
            } else if(line[j] == '1') {
                genotypes[i][j] = 1;
            } else {
                assert(line[j] == '2');
                genotypes[i][j] = 2;
            }
        }
    }

    // get rid of next 2 lines
    getline(in, line);
    getline(in, line);

    // get pairings
    pairings.resize(num_haps/2);

    for(uint i=0; i<num_haps/2; ++i) {
        getline(in, line);
        pairings[i].first.resize(segsites);
        for(uint j=0; j<segsites; ++j) {
            if(line[j] == '0') {
                pairings[i].first[j] = 0;
            } else {
                assert(line[j] == '1');
                pairings[i].first[j] = 1;
            }
        }

        getline(in, line);
        pairings[i].second.resize(segsites);
        for(uint j=0; j<segsites; ++j) {
            if(line[j] == '0') {
                pairings[i].second[j] = 0;
            } else {
                assert(line[j] == '1');
                pairings[i].second[j] = 1;
            }
        }
    }

    in.close();
    return 0;
}
